/**
 * 
 */
package uk.co.altv.simpledb.operation;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import uk.co.altv.simpledb.Connection;

/**
 * @author niki
 *
 */
public class PutAttributes extends Operation{

	private Map<String, ArrayList<Object>> attributes = null;
	private String domain = "";
	
	public PutAttributes(Map<String, ArrayList<Object>> attributes, String domain) {
		this.action = "PutAttributes";
		this.attributes = attributes;
		this.domain = domain;
	}
	
	public String prepareStatement(Connection endpoint) {
		StringBuilder bu = new StringBuilder(4096); int count = 0;
		for (Map.Entry<String, ArrayList<Object>> column: attributes.entrySet()) {
			if (column.getKey() != Key.ITEM_NAME) {
				Iterator<Object> it = column.getValue().iterator();
				while(it.hasNext()) {
					// this is where we will add the filter
					String value = String.valueOf(it.next());
					bu.append(appendData("Attribute."+count+".Name",column.getKey(),endpoint))
						.append(appendData("Attribute."+count+".Value", value, endpoint));
					count++;
				}
			}
		}
		String statement = getIdAndAction(endpoint)
			+ bu.toString()
			+ appendData(Key.DOMAIN_NAME, this.domain, endpoint)
			+ appendData(Key.ITEM_NAME, (String)this.attributes.get(Key.ITEM_NAME).get(0), endpoint)
			+ doTail(endpoint);
		return statement + appendData(Key.SIGNATURE, endpoint.sign(statement), endpoint);
	}

}
